package com.cn.springcloudwechat.service.personalcenter.serviceImpl;

import com.cn.springcloudwechat.common.utils.AgeCount;
import com.cn.springcloudwechat.common.utils.EmptyUtil;
import com.cn.springcloudwechat.common.utils.ErrorUtil;
import com.cn.springcloudwechat.common.utils.ThreadLocalUtil;
import com.cn.springcloudwechat.mapper.mapper.YmChildGrowthMapper;
import com.cn.springcloudwechat.mapper.mapper.YmChildGrowthRepoMapper;
import com.cn.springcloudwechat.mapper.mapper.YmUserChildMapper;
import com.cn.springcloudwechat.model.YmChildGrowth;
import com.cn.springcloudwechat.model.YmChildGrowthRepo;
import com.cn.springcloudwechat.model.YmUserChild;
import com.cn.springcloudwechat.service.personalcenter.service.YmChildGrowthService;
import org.omg.PortableInterceptor.INACTIVE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service("YmChildGrowthService")
public class YmChildGrowthServiceImpl implements YmChildGrowthService{

    @Autowired
    YmChildGrowthMapper ymChildGrowthMapper;

    @Autowired
    YmUserChildMapper ymUserChildMapper;

    @Autowired
    YmChildGrowthRepoMapper ymChildGrowthRepoMapper;


    private YmChildGrowth setYmChildGrowth(YmChildGrowth ymChildGrowth) throws ParseException {
        YmUserChild ymUserChild = this.ymUserChildMapper.selectByPrimaryKey(ymChildGrowth.getChildrenId());
        ymChildGrowth.setChildrenId(ymChildGrowth.getChildrenId());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = sdf.parse(ymUserChild.getChildBirthday());
        String age = AgeCount.getAge(date);
        String[] result = age.split("-");
        Integer y = Integer.valueOf(result[0]);
        Integer m = Integer.valueOf(result[1]);
        String age1=y+"岁"+m+"月";
        ymChildGrowth.setAge(age1);
        Integer month = y*12+m;
        short sex = ymUserChild.getChildSex();
        return ymChildGrowth;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer insertYmChildGrowth(YmChildGrowth ymChildGrowth) throws ParseException, IllegalAccessException {
        Assert.state(!EmptyUtil.objFieldsIsEmpty(ymChildGrowth),String.format(ErrorUtil.PARAMETER_NULL,"ymChildGrowth"));
        ymChildGrowth = setYmChildGrowth(ymChildGrowth);
        return ymChildGrowthMapper.insertSelective(ymChildGrowth);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer updateYmChildGrowth(YmChildGrowth ymChildGrowth) throws ParseException, IllegalAccessException {
        Assert.state(!EmptyUtil.objFieldsIsEmpty(ymChildGrowth),String.format(ErrorUtil.PARAMETER_NULL,"ymChildGrowth"));
        ymChildGrowth = setYmChildGrowth(ymChildGrowth);
        return ymChildGrowthMapper.updateByPrimaryKeySelective(ymChildGrowth);
    }

    @Override
    public List<YmChildGrowth> getYmChildGrowth(Long childId) {
        Assert.notNull(childId,String.format(ErrorUtil.PARAMETER_NULL,"childId"));
        List<YmChildGrowth> result=  ymChildGrowthMapper.getByChildId(childId);
        if(result.size()==1){
            List<YmChildGrowth>  resultList=new ArrayList<YmChildGrowth>();
            //size==1 添加一个数据
            YmChildGrowth ymChildGrowth=new YmChildGrowth();
            ymChildGrowth.setAge("0岁0月");
            ymChildGrowth.setChildrenId(childId);
            ymChildGrowth.setHeight(0d);
            ymChildGrowth.setWeight(0d);
            resultList.add(ymChildGrowth);
            resultList.add(result.get(0));
            return resultList;
        }else{
            return result;
        }
    }

    @Override
    public YmChildGrowth getNewestYmChildGrowth(Long childId) {
        Assert.notNull(childId,String.format(ErrorUtil.PARAMETER_NULL,"childId"));
        return ymChildGrowthMapper.getNewestYmChildGrowth(childId);
    }


    public static void main(String[] args) {
        String a = "207-10-23";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            System.out.println(sdf.parse(a));
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
}
